﻿
#pragma once

#include "GActivity.h"
#include "Common/Timer/TimerAxis.h"
#include "Common/Event/Event.h"
#include "Common/EventDefine.h"

class GActivityAncientElf : public ITimerHandler, public IEventExecuteSink, public GActivity
{
	enum
	{
		ETimer_ID_Check = 1,   //定时器ID

		ENpc_Talk_ContiSec = 5, //npc对话之后持续时间 秒
	};
	enum
	{
		ElfSubType_none = 0,
		ElfSubType_erudite = 1, //博学小精灵 npc
		ElfSubType_greed = 2, //贪婪小精灵 npc
		ElfSubType_irritable = 3, //暴躁的小精灵
		ElfSubType_trouble = 4,	 //捣蛋小精灵
		ElfSubType_sensitive = 5, //敏感小精灵
		ElfSubType_limit,
	};

	enum
	{
		ElfTalk_reply_answer_sucess			= 1, //答题成功
		ElfTalk_reply_give_money_sucess		= 2, //给予金币成功
		ElfTalk_reply_kill_mons_sucess		= 3, //打怪成功
		ElfTalk_reply_in_talk				= 4, //正在对话当中
		ElfTalk_reply_answer_error			= 5, //答题错误
		ElfTalk_reply_give_money_fail		= 6, //给予金币失败
		ElfTalk_reply_already_answer_error  = 7, //答题已经答错过
	};

	typedef unordered_map<uint64_t, AncientElfNpcState> AnCientElfNpcMap;
	typedef unordered_map<uint64_t, AnCientElfMonsState> AnCientElfMonsMap;
public:
	GActivityAncientElf();
	virtual ~GActivityAncientElf();
public:
	virtual void Init(uint32_t activityId);
	virtual void UnInit();
	virtual bool	Update();
	virtual int32_t	GetType() { return ATI_ANCIENT_ELF; };
	virtual void InitUnionParam(uint32_t unionId, uint32_t sceneId, uint32_t mapId, int32_t unionLev, int32_t averageLev, int32_t collegaLev, int32_t vaultLev);
	//玩家进入工会驻地
	virtual void OnEnterUnionHome(CharIDType charId);
	//工会等级更新
	virtual void OnUnionLevUpdate(int32_t unionLev);
	//npc对话
	virtual int32_t TalkToNpc(Player *pPlayer, uint64_t npcCid, int32_t reply,int8_t &type);
	//////////////////////////////////////////////////////////////////////////
	virtual void OnTimer(uint32_t nTimerID);
	//////////////////////////////////////////////////////////////////////////
	virtual void OnExecute(uint16_t nEventID, uint64_t nSrcID, uint8_t bySrcType, void *pContext, int32_t nLen);
protected:	
	virtual void	OnActivityStart();
	virtual void	OnActivityStop();

private:
	int32_t GetNpcNum(int8_t elfType);								//根据类型获取NPC数量
	void GetCanFreshNpcArea(int8_t elfType, SET_UINT32 &setArea);	//根据npc类型获取可刷新区域
	void GetCanFreshMonsArea(int8_t elfType, SET_UINT32 &setArea);	//根据monster类型获取可刷新区域
	AncientElfNpcState *GetAreaNpcByType(uint32_t areaId, int8_t elfType);	//根据类型获取区域NPC
	AnCientElfMonsState *GetAreaMonsByType(uint32_t areaId, int8_t elfType);	//根据类型获取区域怪
	AncientElfNpcState *GetNpc(uint64_t npcCid);					//获取NPC状态信息
	AnCientElfMonsState *GetMons(uint64_t monsCid);					//获取monster状态信息
	void DelNpc(uint64_t npcCid);		//移除npc
	void DelMons(uint64_t monsCid);		//移除monster
	void AddNpc(AncientElfNpcState &npcState);		//添加NPC
	void AddMons(AnCientElfMonsState &monsState);	//添加monster
	void NpcNum(int32_t &erudite,int32_t &greed);							//npc数量
	void MonsNum(int32_t &irritable,int32_t &trouble,int32_t &sensitive);  //mons 数量
	void CheckFresh();		//检查刷新
	void CheckDelNpc();		//检查需要删除的NPC
	uint32_t RandQuestionId(); //随机一个题目ID
	int32_t RandMoney();		//随机一个给予金币数量
	void NotifyPlayerNpcState(Player *pPlayer, ActivityNotifyNpcState &rsp);
	void NotifySceneNpcState(ActivityNotifyNpcState &rsp);
	void SetNpcStateProto(ActMultNpcStateProto &rsp, AncientElfNpcState &npcState);
	void SetNpcStateProto(ActMultNpcStateProto &rsp, AncientElfNpcState &npcState,CharIDType cid);
	void NotifyNpcDie(SET_UINT64 &setDie);
	bool FreshPlanMonster(uint64_t npcCid, Point3<float> midPos, uint32_t areaId, CharIDType charId);	//刷新位面怪
	void AddPlanRelate(uint64_t planUid,uint64_t npcCid);
	void DelPlanRelate(uint64_t planUid);
	uint64_t GetPlanRelate(uint64_t planUid);
	void NpcDie(uint64_t npcCid, int8_t elfType,bool notify);//NPC死亡
	void OnMonsDrop(DieEvent *pDieEvent); //怪物掉落
	void RecycleNpcAndMonster();  //回收所有的NPC和怪物
	void AddDelNpc(uint64_t npcCid); //添加待删除的NPC
	bool IsDelNpc(uint64_t npcCid);	//是否是待移除的NPC
	bool IsAnswerError(AncientElfNpcState *pNpcState, CharIDType cid); //是否已经答错过
	void AnswerError(AncientElfNpcState *pNpcState, Player *pPlayer); //答错题
	void FindBrushPos(Point3<float> midPos, int32_t count, VecPoint3 &vecPos);
	int32_t AddGiveGoldReward(Player *pPlayer); //增加给予金币奖励
	int32_t AddAnswerReward(Player *pPlayer); //增加答题奖励
private:
	void Clear();									//清空	
	void ClearUnionParam();							//清空工会参数
	Monster *GetSceneMonster(CharIDType cid);			//获取怪物
	Npc *GetSceneNpc(CharIDType cid);					//获取npc
	Player*GetScenePlayer(CharIDType cid);				//获取玩家
	void AddHuoyue(SET_UINT64 &setCid);					//增加远古精灵活跃度
	void AddWaitPlan(uint64_t planUid);					//增加等待销毁的位面
	void DelWaitPlan(uint64_t planUid);					//移除等待销毁的位面
	void OnPlanDestory();								//销毁位面
private:
	AncientElfCfg m_cfg;			//远古精灵配置
	uint32_t m_unionId;				//工会ID
	uint32_t m_sceneId;				//场景ID
	uint32_t m_mapId;				//地图ID
	int32_t m_lev;					//工会等级
	int32_t m_averageLev;			//工会精英平均等级
	int32_t m_collegaLev;			//工会学院等级
	AnCientElfNpcMap m_mapNpc;		//npc
	AnCientElfMonsMap m_mapMons;	//monster
	MAP_UINT64_UINT64 m_mapDelNpc;	//待删除的NPC cid
	int32_t m_elfNumAry[ElfSubType_limit];//精灵已经死亡数量
	int32_t m_elfFreshNumAry[ElfSubType_limit];//精灵已经刷新数量
	MAP_UINT64_UINT64 m_mapPlanRelate; //位面关联的NPC planuid - npccid
	SET_UINT64 m_setWaitPlan;		//等待销毁的位面
};